package cn.fancy.sdk.pay.view;

import java.util.List;

import cn.fancy.sdk.common.utils.ResUtil;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AmountDialogHelper extends Dialog {
	private Context context;
	private int position;

	public AmountDialogHelper(Context context) {
		super(context);
		this.context = context;
		
	}

	public AmountDialogHelper(final Context context, List<String> payNOList,
			final View edit) {
		super(context);

		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		// 设置对话框使用的布局文件
		 this.setContentView(ResUtil.getLayoutId(context, "guild_pay_amount_dialog"));

		setCanceledOnTouchOutside(true);
		GridView gridPay =(GridView)this.findViewById(ResUtil.getId(context, "guild_pay_amount_select"));
		//gridPay.setSelector(android.R.color.transparent);
		int sum = payNOList.size();
		if (sum < 3) {
			gridPay.setNumColumns(sum);
		} else {
			gridPay.setNumColumns(3);
			sum = 3;
		}
		MoneyAdapter adapter = new MoneyAdapter(payNOList);
		gridPay.setAdapter(adapter);

		// 为GridView设置监听器
		gridPay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				position = arg2;
				// 点击item后Dialog消失
				AmountDialogHelper.this.dismiss();
				((TextView) edit).setText(arg0.getAdapter().getItem(arg2)
						.toString());
			}
		});
	}

	// 返回点击的位置
	public int getPosition() {
		return position;
	}

	class MoneyAdapter extends BaseAdapter {

		private List<String> payNOList;

		public MoneyAdapter(List<String> priorityList) {
			this.payNOList = priorityList;
		}

		@Override
		public int getCount() {
			return payNOList.size();
		}

		@Override
		public Object getItem(int position) {
			return payNOList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout amountListLayout = (LinearLayout) convertView;
			if (amountListLayout == null) {
				LayoutInflater inflater = LayoutInflater.from(context);
				amountListLayout = (LinearLayout) inflater.inflate(
						ResUtil.getLayoutId(context, "guild_pay_amount_adaper"), null);
			}
			TextView tv = (TextView) amountListLayout
					.findViewById(ResUtil.getId(context, "guild_pay_amount_tv"));
			tv.setText("" + payNOList.get(position));
			return amountListLayout;
		}
	}
}